package com.cloudsigns.factorymanager2.media_system.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.customview.BottomDialog;
import com.cloudsigns.factorymanager2.media_system.activity.MediaSystemMainFragmentActivity;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class MediaSystemMapFragment extends Fragment implements BottomDialog.OnBottomDialogClick,View.OnClickListener{
    private final String TAG = MediaSystemMapFragment.class.getSimpleName();
    private MapView bmapView;
    private BaiduMap mBaiduMap;

    private int currentModuleType = -1;


    private LinearLayout ll_intelligent_device;
    private ImageView img_publication,img_engineering_construct;
    private LinearLayout ll_publication,ll_engineering_construct;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_media_system_map,null);
        ll_intelligent_device=view.findViewById(R.id.ll_intelligent_device);
        img_publication=view.findViewById(R.id.img_publication);
        ll_publication=view.findViewById(R.id.ll_publication);
        img_engineering_construct=view.findViewById(R.id.img_engineering_construct);
        ll_engineering_construct=view.findViewById(R.id.ll_engineering_construct);


        //获取地图控件引用
        bmapView = view.findViewById(R.id.bmapView);
        //不显示缩放控件
        bmapView.showZoomControls(false);
        /*获取地图对象*/
        mBaiduMap = bmapView.getMap();

        resetMapView();
        mBaiduMap.setOnMarkerClickListener(onMarkerClickListener);




        img_publication.setOnClickListener(this);
        img_engineering_construct.setOnClickListener(this);


        return view;
    }

    /*设置地图默认显示坐标点和缩放级别*/
    private void resetMapView() {
        int zoom = 5;
        double latDefault = 34.0;
        double lngDefault = 108.0;
        try {
            if (mBaiduMap != null) {
                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().target(new LatLng(latDefault, lngDefault)).zoom(zoom).build()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*选择一个工程后设置以该项目为中心的地图显示*/
    public void selectEngineering(double lat, double lng) {
        LatLng latLng = new LatLng(lat, lng);
        if (mBaiduMap != null) {
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(17).target(latLng).build()));
        }
    }

    public void getOrderList(int engineeringType) {
        resetMapView();

        currentModuleType = engineeringType;
        mBaiduMap.clear();

        if (currentModuleType == MediaSystemMainFragmentActivity.CONDITION_MONITOR) { // 智能设备
            ll_intelligent_device.setVisibility(View.VISIBLE);
            img_publication.setVisibility(View.GONE);
            img_engineering_construct.setVisibility(View.GONE);
            ll_publication.setVisibility(View.GONE);
            ll_engineering_construct.setVisibility(View.GONE);

           // engineeringOrderListForMapPresenter.getEngineeringOrderList();

        } else if (currentModuleType == MediaSystemMainFragmentActivity.PUBLICATION) { // 广告上刊
            img_publication.setVisibility(View.VISIBLE);
            ll_intelligent_device.setVisibility(View.GONE);
            img_engineering_construct.setVisibility(View.GONE);
            ll_publication.setVisibility(View.GONE);
            ll_engineering_construct.setVisibility(View.GONE);

           // completionOrderListForMapPresenter.getCompletionOrderListForMap();

        } else if (currentModuleType == MediaSystemMainFragmentActivity.ENGINEERING_CONSTRUCTION) { // 工程施工
            img_engineering_construct.setVisibility(View.VISIBLE);
            ll_intelligent_device.setVisibility(View.GONE);
            img_publication.setVisibility(View.GONE);
            ll_publication.setVisibility(View.GONE);
            ll_engineering_construct.setVisibility(View.GONE);

        }
    }

    private void updataMap() {
        if (currentModuleType == MediaSystemMainFragmentActivity.CONDITION_MONITOR) { // 智能设备
//            if (null != engineeringOrderList) {
//                addMarker(engineeringOrderList);
//            }
        } else if (currentModuleType == MediaSystemMainFragmentActivity.PUBLICATION) { // 广告上刊

        } else if (currentModuleType == MediaSystemMainFragmentActivity.ENGINEERING_CONSTRUCTION) { // 工程施工

        }
//        else if (currentModuleType == EngineeringMainForManagerActivity.MODULE_YEJI) { //业绩
//            if (null != completionOrderList) {
//                addMarkerForYeji(completionOrderList);
//            }
//        }
    }

    /*往地图添加一个点*/

    private void addMarker(List<OrderParentBean> orderParentBeanList) {
        //创建OverlayOptions的集合
        List<OverlayOptions> options = new ArrayList<OverlayOptions>();
        //构建Marker图标
        BitmapDescriptor bitmap_exception = null;
        BitmapDescriptor bitmap_wait_confirm = null;
        BitmapDescriptor bitmap_working = null;
        BitmapDescriptor bitmap_complete = null;
        BitmapDescriptor bitmap_out_plan = null;
        BitmapDescriptor bitmap_no_feedback = null;
        BitmapDescriptor bitmap_pause = null;
        BitmapDescriptor bitmap_create = null;

        LatLng point;
        Bundle bundle = null;
        String title = "";
        BitmapDescriptor bitmap = null;

        for (int i = 0; i < orderParentBeanList.size(); i++) {
            bitmap = null;

            OrderParentBean orderParentBean = orderParentBeanList.get(i);

            bundle = new Bundle();
            bundle.putSerializable(ConstantUtil.KEY_1, orderParentBean);

            CommonEnum.EngineeringORderCategory oRderCategory = orderParentBean.getCategory();
            if (null != oRderCategory) {
                if (oRderCategory == CommonEnum.EngineeringORderCategory.INSTALL) {
                    title = "工程安装";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                    title = "批量工程";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                    title = "施工";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                    title = "维修";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                    title = "上刊";
                    ;
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.PATROL) {
                    title = "巡查";
                } else if (oRderCategory == CommonEnum.EngineeringORderCategory.SURVEY) {
                    title = "勘查";
                }
            }

//            //设置坐标点
//            int j = i % 16;
//            point = new LatLng(latList.get(j), lonList.get(j));
            point = new LatLng(orderParentBean.getLat(), orderParentBean.getLon());

            OverlayOptions option = null;

            CommonEnum.FeedBackState feedBackState = orderParentBean.getFeedBackState();
            if (CommonEnum.FeedBackState.EXCEPTION == feedBackState) {
                if (null == bitmap_exception) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_2);
                    //构建Marker图标
                    bitmap_exception = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }

                bitmap = bitmap_exception;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);

            } else if (CommonEnum.FeedBackState.WAIT_CONFIRM == feedBackState) {
                if (null == bitmap_wait_confirm) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_1);
                    //构建Marker图标
                    bitmap_wait_confirm = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }

                bitmap = bitmap_wait_confirm;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.WORKING == feedBackState) {
                if (null == bitmap_working) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_3);
                    //构建Marker图标
                    bitmap_working = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_working;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.COMPLETE == feedBackState) {
                if (null == bitmap_complete) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_7);
                    //构建Marker图标
                    bitmap_complete = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_complete;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.OUT_OF_PLAN == feedBackState) {
                if (null == bitmap_out_plan) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_4);
                    //构建Marker图标
                    bitmap_out_plan = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_out_plan;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.NO_FEEDBACK == feedBackState) {
                if (null == bitmap_no_feedback) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_5);
                    //构建Marker图标
                    bitmap_no_feedback = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_no_feedback;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.PAUSE == feedBackState) {
                if (null == bitmap_pause) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_6);
                    //构建Marker图标
                    bitmap_pause = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_pause;

//                //在地图上添加Marker，并显示
//                mBaiduMap.addOverlay(option);
            } else if (CommonEnum.FeedBackState.CREATE == feedBackState) {
                if (null == bitmap_create) {
                    Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_8);
                    //构建Marker图标
                    bitmap_create = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
                }
                bitmap = bitmap_create;
            }

            //创建OverlayOptions属性
            if (null != bitmap)
                option = new MarkerOptions().position(point).title(title).extraInfo(bundle).icon(bitmap);

            //将OverlayOptions添加到list
            if (null != option)
                options.add(option);
        }

        //在地图上批量添加
        mBaiduMap.addOverlays(options);
    }

    /*往地图添加一个点*/

    private void addMarkerForYeji(List<OrderParentBean> orderParentBeanList) {

        //创建OverlayOptions的集合
        List<OverlayOptions> options = new ArrayList<OverlayOptions>();
        //构建Marker图标
        BitmapDescriptor bitmap_icon = null;

        LatLng point;
        Bundle bundle = null;
        String title = "";

        for (int i = 0; i < orderParentBeanList.size(); i++) {

            OrderParentBean orderParentBean = orderParentBeanList.get(i);

            bundle = new Bundle();
            bundle.putSerializable(ConstantUtil.KEY_1, orderParentBean);

//            //设置坐标点
//            int j = i % 16;
//            point = new LatLng(latList.get(j), lonList.get(j));
            point = new LatLng(orderParentBean.getLat(), orderParentBean.getLon());

            OverlayOptions option = null;

            if (null == bitmap_icon) {
                Bitmap tmpBitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.mipmap.icon_map_2);

                //构建Marker图标
                bitmap_icon = BitmapDescriptorFactory.fromBitmap(tmpBitmap);
            }

            //创建OverlayOptions属性
            if (null != bitmap_icon)
                option = new MarkerOptions().position(point).title(title).extraInfo(bundle).icon(bitmap_icon);

            //将OverlayOptions添加到list
            if (null != option)
                options.add(option);
        }

        //在地图上批量添加
        mBaiduMap.addOverlays(options);
    }

    BaiduMap.OnMarkerClickListener onMarkerClickListener = new BaiduMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(final Marker marker) {
            Bundle bundle = marker.getExtraInfo();
            if (null != bundle) {
                if (currentModuleType == MediaSystemMainFragmentActivity.CONDITION_MONITOR) {
                    OrderParentBean orderParentBean = (OrderParentBean) bundle.getSerializable(ConstantUtil.KEY_1);

                    selectEngineering(orderParentBean.getLat(), orderParentBean.getLon());

                    BottomDialog dialog = new BottomDialog(getActivity(), orderParentBean);
                    dialog.setBottomDialogClick(MediaSystemMapFragment.this);
                    dialog.show();

                } else if (currentModuleType == MediaSystemMainFragmentActivity.PUBLICATION) {


                } else if (currentModuleType == MediaSystemMainFragmentActivity.ENGINEERING_CONSTRUCTION) {

                }
            }
            return true;
        }
    };


    @Override
    public void onDialogClick(OrderParentBean orderParentBean) {
        if (null != orderParentBean) {
            CommonEnum.FeedBackState feedBackState = orderParentBean.getFeedBackState();
            Intent mIntent;
            if (CommonEnum.FeedBackState.EXCEPTION == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
////                        mIntent = new Intent(getActivity(), EngineeringFeedBackForExceptionStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
//                startActivity(mIntent);

            } else if (CommonEnum.FeedBackState.WAIT_CONFIRM == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
//                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

            } else if (CommonEnum.FeedBackState.WORKING == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
////                        mIntent = new Intent(getActivity(), EngineeringFeedBackForWorkingStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                startActivity(mIntent);

            } else if (CommonEnum.FeedBackState.COMPLETE == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
////                        mIntent = new Intent(getActivity(), EngineeringFeedBackForCompletionStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
//                startActivity(mIntent);

            } else if (CommonEnum.FeedBackState.OUT_OF_PLAN == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
//                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

            } else if (CommonEnum.FeedBackState.NO_FEEDBACK == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
//                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);

            } else if (CommonEnum.FeedBackState.PAUSE == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
////                        mIntent = new Intent(getActivity(), EngineeringFeedBackForPauseStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
//                startActivity(mIntent);
            } else if (CommonEnum.FeedBackState.CREATE == feedBackState) {
//                mIntent = new Intent(getActivity(), EngineeringInstallFeedBackForWaitStateActivity.class);
////                        mIntent = new Intent(getActivity(), EngineeringFeedBackForPauseStateActivity.class);
//                mIntent.putExtra(ConstantUtil.KEY_1, orderParentBean);
//                mIntent.putExtra(ConstantUtil.KEY_2, TAG);
//                startActivity(mIntent);
            }
        }

    }

    @Override
    public void onClick(View view) {
switch (view.getId()){
    // 广告上刊状态按钮
    case R.id.img_publication:
        if (ll_publication.getVisibility() == View.VISIBLE) {
            ll_publication.setVisibility(View.GONE);
          //  img_abnormal_return.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_publication.setVisibility(View.VISIBLE);
           // img_abnormal_return.setBackgroundResource(R.mipmap.arrow_top);
        }
        break;
        // 工程施工状态按钮
    case R.id.img_engineering_construct:
        if (ll_engineering_construct.getVisibility() == View.VISIBLE) {
            ll_engineering_construct.setVisibility(View.GONE);
           // img_abnormal_return.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_construct.setVisibility(View.VISIBLE);
           // img_abnormal_return.setBackgroundResource(R.mipmap.arrow_top);
        }
        break;

}
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    getOrderList(currentModuleType);
                    break;

            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        bmapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        bmapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        bmapView.onDestroy();
    }



}
